﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "UiViewEventHandler.h"
#include "IUiViewEventArgs.h"
#include "IUiView.h"
#include "ICanvas.h"
#include "ICanvasOptions.h"
#include "IModelView.h"
#include "CommandIds.h"
#include "IUiViewMouseEventHandler.h"
#include "IUiViewMouseEvent.h"
#include "IUiViewMouseEventArgs.h"
#include "IMainWindow.h"
#include "IStatusBar.h"
#include "IStatusBarPanel.h"
#include "ILabelDefinition.h"
#include "IControl.h"
#include "IRibbonContextualPage.h"

#include "UiPlatformUtils.h"
#include "IUiDocumentViewManager.h"
#include "IUiViewEvent.h"
#include "UiViewMouseEventHandler.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace Sample;
using namespace gcmp;

UiViewEventHandler::UiViewEventHandler()
{
    IUiDocumentViewManager *pUiDocumentViewManager = UiPlatformUtils::GetUiDocumentViewManager();
    if (pUiDocumentViewManager)
    {
        IUiViewEvent *event = pUiDocumentViewManager->GetUiViewEvent();
        if (event)
        {
            event->Add(this);
        }
    }
    m_opUiViewMouseEventHandler = NEW_AS_OWNER_PTR(UiViewMouseEventHandler);
}

UiViewEventHandler::~UiViewEventHandler()
{
    IUiDocumentViewManager *pUiDocumentViewManager = UiPlatformUtils::GetUiDocumentViewManager();
    if (pUiDocumentViewManager)
    {
        IUiViewEvent *event = pUiDocumentViewManager->GetUiViewEvent();
        if (event)
        {
            event->Delete(this);
        }
    }
}

void UiViewEventHandler::On(IUiViewEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs,L"Ui视图事件参数不能为空",L"GDMP",L"2023-09-30");
    if (pArgs->GetType() == gcmp::UiViewEventType::UiViewCreated)
    {
        IUiView *pUiView = pArgs->GetUiView();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiView,L"获取Ui视图失败",L"GDMP",L"2023-09-30");
        pUiView->GetUiViewMouseEvent()->Add(m_opUiViewMouseEventHandler.get());
        //设置视图缺省命令
        pUiView->SetDefaultActionCommand(COMMAND_SELECT_SELECTOR);
        //设置视图画布
        ICanvas *pCanvas = pUiView->GetCanvas();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCanvas, L"获取画布失败", L"GDMP", L"2023-09-30");
        ICanvasOptions *pCanvasOption = pCanvas->GetOptions();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiView, L"获取画布选项失败", L"GDMP", L"2023-09-30");
        IModelView *pModelView = pUiView->GetModelView();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pModelView, L"获取模型视图失败", L"GDMP", L"2023-09-30");
        BuiltInViewType viewType = pModelView->GetViewType();
        if (viewType == BuiltInViewType::Drawing || viewType == BuiltInViewType::TwoDimensional)
        {
            pCanvas->SetBackgroundColor(Color::White);
            pCanvasOption->HideViewAxis();
            //开启批次合并
            pCanvasOption->EnableRenderPatchCombine(true, RenderPatchCombineStrategy::Maximum);
            //启用反走样
            pCanvasOption->EnableFullSceneAntiAliasing(true);
        }
        else
        {
            //设置视图画布背景色
            pCanvas->SetBackgroundColor(Color(225, 240, 234), Color(157, 206, 206),
                Color(230, 242, 230), Color(214, 235, 215));
            //设置灯光策略
            pCanvas->SetLightingScheme(LightingScheme::HeadLamp);
            //设置画布选项设置
            
            //显示坐标轴
            pCanvasOption->ShowViewAxis();
            //设置视图旋转时是否允许翻转
            pCanvasOption->SetOrbitalRotationFlippable(true);
            pCanvasOption->SetZoomInLimit(0.1f);
            //开启批次合并
            pCanvasOption->EnableRenderPatchCombine(true, RenderPatchCombineStrategy::Maximum);
            pCanvasOption->SetMinFramePerSecond(0);   //该参数越高越流畅，但模型会闪烁
            //设置LOD
            pCanvasOption->SetAutoLodThreshold(AutoLodLevel::Box, 20);
            pCanvasOption->SetAutoLodThreshold(AutoLodLevel::Hide, 10);
            //启用VBO渲染
            //TODO:临时注释，window远程连接会导致程序崩溃
            //pCanvasOption->EnableVbo(true);
            //启用反走样
            pCanvasOption->EnableFullSceneAntiAliasing(true);
            //显示渲染性能面板
            pCanvasOption->ShowRenderInformationPanel();
        }
    } 
    else if (pArgs->GetType() == UiViewEventType::UiViewActivated)
    {       
        
    }
    else if (pArgs->GetType() == UiViewEventType::UiViewReady)
    {
        
    }
    else if (pArgs->GetType() == UiViewEventType::UiViewClosed)
    {
        IUiView *pUiView = pArgs->GetUiView();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiView, L"获取Ui视图失败", L"GDMP", L"2023-09-30");
        pUiView->GetUiViewMouseEvent()->Delete(m_opUiViewMouseEventHandler.get());
    }
}